package com.pig4cloud.pig.module.controller.intermediary;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.intermediary.IntermediaryOrder;
import com.pig4cloud.pig.module.service.intermediary.IIntermediaryOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 中介信息（房屋、车位）订单Controller
 *
 * @author ruoyi
 * @date 2022-08-26
 */
@Validated
@Api(value = "中介信息（房屋、车位）订单控制器", tags = {"中介信息（房屋、车位）订单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("module/lease/order")
public class IntermediaryOrderController {

    private final IIntermediaryOrderService intermediaryOrderService;

    /**
     * 查询中介信息（房屋、车位）订单列表
     */
    @ApiOperation("查询中介信息（房屋、车位）订单列表")
    @PreAuthorize("@pms.hasPermission('module:leaseOrder:list')")
    @GetMapping("/list")
    public TableDataInfo<IntermediaryOrder> list(@Validated IntermediaryOrder intermediaryOrder) {
        QueryWrapper<IntermediaryOrder> qw = new QueryWrapper<>();
        if(intermediaryOrder.getInfoType() !=null){
            qw.eq("info_type",intermediaryOrder.getInfoType());
        }else {
            qw.eq("info_type",0);
        }
        if(intermediaryOrder.getEstateId()!=null){
            qw.eq("estate_Id",intermediaryOrder.getEstateId());
        }
        qw.orderByDesc("create_time");

        TableDataInfo<IntermediaryOrder> td = PageUtils.buildDataInfo(intermediaryOrderService.page(PageUtils.buildPage(),qw));
        List<IntermediaryOrder> list = td.getData();
        list.forEach(order->{
//            if(StrUtil.isNotEmpty(order.getNickName())){
//                order.setNickName(EmojiParser.parseToUnicode(order.getNickName()));
//            }
        });
        return td;
    }

    /**
     * 导出中介信息（房屋、车位）订单列表
     */
    @ApiOperation("导出中介信息（房屋、车位）订单列表")
    @PreAuthorize("@pms.hasPermission('module:leaseOrder:export')")
    @SysLog("中介信息（房屋、车位）订单")
    @GetMapping("/export")
    public List export(@Validated IntermediaryOrder order) {
        QueryWrapper<IntermediaryOrder> qw = new QueryWrapper<>();
        if(order.getInfoType() !=null){
            qw.eq("info_type",order.getInfoType());
        }
        List<IntermediaryOrder> list = intermediaryOrderService.list(qw);
        // ExcelUtil<IntermediaryOrder> util = new // ExcelUtil<IntermediaryOrder>(IntermediaryOrder.class);
        return  list;// util.exportExcel(list, "中介信息（房屋、车位）订单");
    }

    /**
     * 获取中介信息（房屋、车位）订单详细信息
     */
    @ApiOperation("获取中介信息（房屋、车位）订单详细信息")
    @PreAuthorize("@pms.hasPermission('module:leaseOrder:query')")
    @GetMapping("/{id}")
    public R<IntermediaryOrder> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        IntermediaryOrder intermediaryOrder = intermediaryOrderService.getById(id);
        return R.ok(intermediaryOrder);
    }

    /**
     * 新增中介信息（房屋、车位）订单
     */
    @ApiOperation("新增中介信息（房屋、车位）订单")
    @PreAuthorize("@pms.hasPermission('module:leaseOrder:add')")
    @SysLog("中介信息（房屋、车位）订单")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody IntermediaryOrder intermediaryOrder) {
        return R.okByRow(intermediaryOrderService.save(intermediaryOrder) ? 1 : 0);
    }

    /**
     * 修改中介信息（房屋、车位）订单
     */
    @ApiOperation("修改中介信息（房屋、车位）订单")
    @PreAuthorize("@pms.hasPermission('module:leaseOrder:edit')")
    @SysLog("中介信息（房屋、车位）订单")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody IntermediaryOrder intermediaryOrder) {
        return R.okByRow(intermediaryOrderService.updateById(intermediaryOrder) ? 1 : 0);
    }

    /**
     * 删除中介信息（房屋、车位）订单
     */
    @ApiOperation("删除中介信息（房屋、车位）订单")
    @PreAuthorize("@pms.hasPermission('module:leaseOrder:remove')")
    @SysLog("中介信息（房屋、车位）订单")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(intermediaryOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
